package com.add.petback.controller;

import com.add.petback.model.Complainment;
import com.add.petback.model.Pet;
import com.add.petback.service.PetService;
import com.add.petback.service.impl.ComplainmentServiceImpl;
import com.add.petback.service.impl.PetServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import util.JsonTransformation;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/seller")
public class SellerController {

    @Autowired(required = false)
    private PetServiceImpl petservice;

    @Autowired(required = false)
    private ComplainmentServiceImpl compservice;

    @PostMapping("/upload")
    @ResponseBody
    public String uploadpet(@RequestBody String str) {
        Pet pet = JsonTransformation.jsonToObject(str, Pet.class);
        try
        {
            petservice.insert(pet);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return "404";
        }
        return "200";

    }


    @PostMapping("/delete")
    @ResponseBody
    public String deletepet(@RequestBody String str) {
        Pet pet = JsonTransformation.jsonToObject(str, Pet.class);
        try
        {
            petservice.delete(pet);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return "404";
        }
        return "200";

    }

    @PostMapping("/edit")
    @ResponseBody
    public String editpet(@RequestBody String str) {
        Pet pet = JsonTransformation.jsonToObject(str, Pet.class);
        try
        {
            petservice.save(pet);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return "404";
        }
        return "200";
    }

    @GetMapping("/goods")
    @ResponseBody
    public String search() {
        List<Pet> result = new ArrayList<>();
        List<Pet> pet = petservice.getAll();
        for (Pet newpet : pet) {
            if (newpet.getStatus().equals("在售"))
                result.add(newpet);
        }
        if (pet == null)
            return "404";
        return JsonTransformation.listToJson(result);
    }

    @GetMapping("/complainment")
    @ResponseBody
    public String checkcom() {
        List<Complainment> com = compservice.getAll();

        if (com == null)
            return "404";
        return JsonTransformation.listToJson(com);
    }

    @GetMapping("/checkorder")
    @ResponseBody
    public String checkorder() {
        Pet pet = new Pet();
        List<Pet> result = new ArrayList<>();
        List<Pet> foundPet = petservice.getAll();
        for (Pet newpet : foundPet) {
            if (newpet.getStatus().equals("已付款"))
                result.add(newpet);
        }
        // 如果查找到的商品为空则返回404
        if (foundPet == null)
            return "404";
        return JsonTransformation.listToJson(result);
    }



}
